package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Question;
import cn.wolfcode.wolf2w.business.api.domain.QuestionContent;
import cn.wolfcode.wolf2w.business.mapper.QuestionContentMapper;
import cn.wolfcode.wolf2w.business.mapper.QuestionMapper;
import cn.wolfcode.wolf2w.business.query.QuestionQuery;
import cn.wolfcode.wolf2w.business.service.IQuestionService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 提问Service业务层处理
 * 
 * @author hyz
 * @date 2024-08-13
 */
@Service
@Transactional
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper,Question> implements IQuestionService {


    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private RemoteUserInfoService remoteUserInfoService;

    @Autowired
    private QuestionContentMapper questionContentMapper;

    @Override
    public IPage<Question> queryPage(QuestionQuery qo) {
        IPage<Question> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
//        return lambdaQuery().page(page);
        QueryWrapper<Question> qw = new QueryWrapper<>();
        qw.orderByDesc(qo.getOrderBy());
        IPage<Question> pageResult = questionMapper.selectPage(page, qw);
        for (Question record : pageResult.getRecords()) {
            R<UserInfo> one = remoteUserInfoService.getOne(record.getAuthorId(), SecurityConstants.INNER);
            record.setAuthor(one.getData());
            record.setContent(questionContentMapper.selectById(record.getId()).getContent());
        }
        return pageResult;
    }

    @Override
    public Question queryById(Long id) {
        Question question = questionMapper.selectById(id);
        question.setAuthor(remoteUserInfoService.getOne(question.getAuthorId(), SecurityConstants.INNER).getData());
        question.setContent(questionContentMapper.selectById(id).getContent());
        return question;
    }
}
